#include "http_servlet_container.h"
#include "web_sock_holder.h"

namespace vmon_web
{
    HttpServletContainer::HttpServletContainer()
    {
    }

    HttpServletContainer::~HttpServletContainer()
    {
        // delete all servlets
        std::list<HttpServlet*>::iterator iter = m_servlets.begin();
        while (iter != m_servlets.end())
        {
            delete *iter;
            ++iter;
        }
    }

    void HttpServletContainer::addServlet(HttpServlet* servlet)
    {
        m_servlets.push_back(servlet);
    }

	bool HttpServletContainer::handleRequest(WebSockHolder* sockHolder)
	{
		std::list<HttpServlet*>::iterator iter = m_servlets.begin();
        while (iter != m_servlets.end())
        {
            HttpServlet* servlet = *iter;
            bool rc = servlet->handleRequest(sockHolder);
			if (sockHolder->getResponse().isHandled())
                return rc;
            ++iter;
        }
		return false;
	}

    bool HttpServletContainer::handleRequest(HttpRequest& request, HttpResponse& response)
    {
        std::list<HttpServlet*>::iterator iter = m_servlets.begin();
        while (iter != m_servlets.end())
        {
            HttpServlet* servlet = *iter;
            bool rc = servlet->handleRequest(request, response);
            if (response.isHandled())
                return rc;
            ++iter;
        }
        return false;
    }

}
